package com.excesys.exsecs.admin.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.admin.api.entity.SysCompConf;
import com.excesys.exsecs.admin.service.SysCompConfService;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.log.annotation.SysLog;
import org.springframework.security.access.prepost.PreAuthorize;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * 系统组件配置
 *
 * @author luxinchao
 * @date 2024-11-13 15:23:02
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sysCompConf" )
@Tag(description = "sysCompConf" , name = "系统组件配置管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SysCompConfController {

    private final SysCompConfService sysCompConfService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param sysCompConf 系统组件配置
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('sys_sysCompConf_view')" )
    public R getSysCompConfPage(@ParameterObject Page page, @ParameterObject SysCompConf sysCompConf) {
        LambdaQueryWrapper<SysCompConf> wrapper = Wrappers.lambdaQuery();
        return R.ok(sysCompConfService.page(page, wrapper));
    }


    /**
     * 通过id查询系统组件配置
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('sys_sysCompConf_view')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(sysCompConfService.getById(id));
    }

    /**
     * 新增系统组件配置
     * @param sysCompConf 系统组件配置
     * @return R
     */
    @Operation(summary = "新增系统组件配置" , description = "新增系统组件配置" )
    @SysLog("新增系统组件配置" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sys_sysCompConf_add')" )
    public R save(@RequestBody SysCompConf sysCompConf) {
        long count = sysCompConfService.count(Wrappers.<SysCompConf>lambdaQuery().eq(SysCompConf::getCompZh, sysCompConf.getCompZh().trim())
                .or().eq(SysCompConf::getCompEn, sysCompConf.getCompEn().trim()));
        if (count > 0){
            return R.failed("组件已存在");
        }
        sysCompConf.setCompCode(UUID.randomUUID().toString().replace("-", "").substring(0, 8));
        boolean bool = sysCompConfService.save(sysCompConf);
        if (bool){
            return R.ok();
        } else{
            return R.failed("新增失败");
        }
    }

    /**
     * 修改系统组件配置
     * @param sysCompConf 系统组件配置
     * @return R
     */
    @Operation(summary = "修改系统组件配置" , description = "修改系统组件配置" )
    @SysLog("修改系统组件配置" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('sys_sysCompConf_edit')" )
    public R updateById(@RequestBody SysCompConf sysCompConf) {
        return R.ok(sysCompConfService.updateById(sysCompConf));
    }

    /**
     * 通过id删除系统组件配置
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除系统组件配置" , description = "通过id删除系统组件配置" )
    @SysLog("通过id删除系统组件配置" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('sys_sysCompConf_del')" )
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(sysCompConfService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param sysCompConf 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('sys_sysCompConf_export')" )
    public List<SysCompConf> export(SysCompConf sysCompConf,Integer[] ids) {
        return sysCompConfService.list(Wrappers.lambdaQuery(sysCompConf).in(ArrayUtil.isNotEmpty(ids), SysCompConf::getId, ids));
    }
}